﻿Imports TransportadoraToledoBLL

Public Class frm_contas_a_receber

    Dim crCodigo As Integer

    Private Sub bPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar.Click
        Dim objBll As New ContasReceberBLL

        If (dtInicio.Value.Date > dtFim.Value.Date) Then
            MsgBox("Data Inicial Maior Que Final")
        Else
            Try
                dgvDados.DataSource = objBll.localizaAbertos(dtInicio.Value, dtFim.Value)
                dgvDados.Columns(1).Width = 50
                dgvDados.Columns(2).Width = 180
                dgvDados.Columns(4).DefaultCellStyle.Format = "R$ ###,###,##0.00"
                dgvDados.Columns(0).ReadOnly = False
                dgvDados.Columns(1).ReadOnly = True
                dgvDados.Columns(2).ReadOnly = True
                dgvDados.Columns(3).ReadOnly = True
                dgvDados.Columns(4).ReadOnly = True
            Catch ex As Exception
                Throw ex
            End Try
        End If
    End Sub

    Private Sub tbEmAberto_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbEmAberto.SelectedIndexChanged

        Try
            If tbEmAberto.SelectedIndex() = 0 Then
                Me.Size = New Size(712, 626)
                bPesquisar_Click(sender, e)
            End If
            If tbEmAberto.SelectedIndex() = 1 Then
                Me.Size = New Size(712, 626)
                'Me.Size = New Size(1108, 626)
                bPesquisa2_Click(sender, e)
            End If
            If tbEmAberto.SelectedIndex() = 2 Then
                Me.Size = New Size(712, 626)
                Button5_Click(sender, e)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub frm_contas_a_receber_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(712, 626)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar3.Click
        Dim objBll As New ContasReceberBLL
        If (dtinicio3.Value.Date > dtfim3.Value.Date) Then
            MsgBox("Data Inicial Maior Que Final")
        Else
            Try
                dg_dados3.DataSource = objBll.LozalizaLancados(dtinicio3.Value, dtfim3.Value)
                dg_dados3.Columns(1).Width = 50
                dg_dados3.Columns(2).Width = 180
                dg_dados3.Columns(4).DefaultCellStyle.Format = "R$ ###,###,##0.00"
                dg_dados3.Columns(0).ReadOnly = False
                dg_dados3.Columns(1).ReadOnly = True
                dg_dados3.Columns(2).ReadOnly = True
                dg_dados3.Columns(3).ReadOnly = True
                dg_dados3.Columns(4).ReadOnly = True
            Catch ex As Exception
                Throw ex
            End Try
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim objBll As New ContasReceberBLL

        Dim cliCodigo As Integer
        Dim formaPgto As String
        Dim agendamento As Integer
        Dim valorTotal As Decimal
        Dim dataLancamento As Date
        Dim cliNome As String
        Dim dataCompara As String

        Dim cr_codigo As Integer
        Dim pr_valor As Decimal
        Dim pr_d_vencimento As Date
        Dim pr_status_pg As Char
        Dim pr_parcela As Integer


        Dim frm As New frm_parcelas

        For Each col As DataGridViewRow In dgvDados.Rows
            dgvDados.ReadOnly = True

            If col.Cells(0).Value = True Then
                agendamento = col.Cells(1).Value
                cliCodigo = col.Cells(2).Value
                cliNome = col.Cells(3).Value
                valorTotal = col.Cells(4).Value
                dataCompara = col.Cells(5).Value
                frm.lancamento = agendamento
                frm.valTot = valorTotal
                frm.dataPgto = dataCompara

                frm.ShowDialog()
                formaPgto = frm.forma
                dataLancamento = frm.dataPgto


                If formaPgto = "" Then
                    MessageBox.Show("ESCOLHA UMA FORMA DE PAGAMENTO!")
                ElseIf (dataLancamento < dataCompara) Then
                    MessageBox.Show("DATA DE PAGAMENTO INVALIDA!")
                Else

                    objBll.inserirLancamentos(cliCodigo, formaPgto, agendamento, valorTotal, dataLancamento, cliNome)


                    If (formaPgto = "DUPLICATA") Then
                        cr_codigo = objBll.pCodigoContasReceber
                        For Each colun As DataGridViewRow In frm.gd_parcelas.Rows

                            pr_parcela = colun.Cells(0).Value
                            pr_d_vencimento = colun.Cells(1).Value
                            pr_valor = colun.Cells(2).Value

                            pr_status_pg = "A"

                            objBll.InserirParcelas(cr_codigo, pr_valor, pr_d_vencimento, pr_status_pg, pr_parcela)
                        Next
                    End If
                End If
            End If
        Next

        frm = Nothing
        dgvDados.ReadOnly = False

        bPesquisar_Click(sender, e)
    End Sub

    Private Sub bPesquisa2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisa2.Click
        If (dtInicio2.Value.Date > dtfinal2.Value.Date) Then
            MsgBox("Data Inicial Maior Que Final")
        Else
            Dim objBll As New ContasReceberBLL
            dgDados2.DataSource = objBll.LocalizaParcelados(dtInicio2.Value, dtfinal2.Value)
            dgDados2.Columns(4).DefaultCellStyle.Format = "R$ ###,###,##0.00"
            objBll = Nothing
            If (dgParcAbertos.SelectedRows.Count >= 0) Then
                dgParcAbertos.DataSource = Nothing
            End If
            If (dgParcBaixados.SelectedRows.Count >= 0) Then
                dgParcBaixados.DataSource = Nothing
            End If
        End If
    End Sub

    Private Sub dg_dados3_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dg_dados3.DataBindingComplete
        Dim total As Decimal
        For Each col As DataGridViewRow In dg_dados3.Rows
            total = total + col.Cells(4).Value
        Next
        txtRecedido.Text = Format(total, "R$ ###,###,##0.00").ToString()
    End Sub

    Private Sub dgDados2_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgDados2.DataBindingComplete
        Dim total As Decimal
        For Each col As DataGridViewRow In dgDados2.Rows
            total = total + col.Cells(4).Value
        Next
        txtParcelados.Text = Format(total, "R$ ###,###,##0.00").ToString()
    End Sub

    Private Sub dgvDados_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgvDados.DataBindingComplete
        Dim total As Decimal
        For Each col As DataGridViewRow In dgvDados.Rows
            total = total + col.Cells(4).Value
        Next
        txtAberto.Text = Format(total, "R$ ###,###,##0.00").ToString()
    End Sub

    Private Sub bSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSair.Click
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub DataGridView1_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgParcAbertos.DataBindingComplete
        Dim total As Decimal
        For Each col As DataGridViewRow In dgParcAbertos.Rows
            total = total + col.Cells(4).Value
        Next
        txtParcAberto.Text = Format(total, "R$ ###,###,##0.00").ToString()
    End Sub

    Private Sub dgParcBaixados_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgParcBaixados.DataBindingComplete
        Dim total As Decimal
        For Each col As DataGridViewRow In dgParcBaixados.Rows
            total = total + col.Cells(2).Value
        Next
        txtParcBaixados.Text = Format(total, "R$ ###,###,##0.00").ToString()
    End Sub


    Private Sub dgDados2_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgDados2.CellClick

        Dim objBll As New ContasReceberBLL

        Dim coluna As Integer
        Dim linha As Integer

        Dim indiceselecionado As Integer = -1
        'Se o usuário selecionou a Linha
        If (dgDados2.SelectedRows.Count > 0) Then
            indiceselecionado = dgDados2.SelectedRows(0).Index
        End If

        If (dgDados2.SelectedCells.Count > 0) Then

            indiceselecionado = dgDados2.SelectedCells(0).RowIndex
        End If

        If (indiceselecionado <> -1) Then
            Me.Size = New Size(1108, 626)
            coluna = dgDados2.CurrentCell.ColumnIndex
            linha = dgDados2.CurrentRow.Index
            crCodigo = dgDados2.Rows(linha).Cells(0).Value
            dgParcAbertos.DataSource = objBll.LocalizaParcelasAbertas(crCodigo)
            dgParcAbertos.Columns(4).DefaultCellStyle.Format = "R$ ###,###,##0.00"
            dgParcAbertos.Columns(0).ReadOnly = False
            dgParcAbertos.Columns(1).ReadOnly = True
            dgParcAbertos.Columns(2).ReadOnly = True
            dgParcAbertos.Columns(3).ReadOnly = True
            dgParcAbertos.Columns(4).ReadOnly = True

            dgParcBaixados.DataSource = objBll.LocalizaParcelasBaixados(crCodigo)
            dgParcBaixados.Columns(2).DefaultCellStyle.Format = "R$ ###,###,##0.00"
        End If

        objBll = Nothing
    End Sub

    Private Sub bBaixar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bBaixar.Click
        Dim objBll As New ContasReceberBLL
        Try
            Dim pr_codigo As Integer

            For Each col As DataGridViewRow In dgParcAbertos.Rows
                dgParcAbertos.ReadOnly = True

                pr_codigo = col.Cells(1).Value

                If col.Cells(0).Value = True Then
                    objBll.BaixarParcelas(pr_codigo)
                End If
            Next

        Catch ex As Exception

        End Try
        dgParcAbertos.ReadOnly = False
        'dgParcAbertos.DataSource = Nothing
        'dgParcBaixados.DataSource = Nothing
        dgParcAbertos.DataSource = objBll.LocalizaParcelasAbertas(crCodigo)
        dgParcAbertos.Columns(4).DefaultCellStyle.Format = "R$ ###,###,##0.00"
        dgParcAbertos.Columns(0).ReadOnly = False
        dgParcAbertos.Columns(1).ReadOnly = True
        dgParcAbertos.Columns(2).ReadOnly = True
        dgParcAbertos.Columns(3).ReadOnly = True
        dgParcAbertos.Columns(4).ReadOnly = True

        dgParcBaixados.DataSource = objBll.LocalizaParcelasBaixados(crCodigo)
        dgParcBaixados.Columns(2).DefaultCellStyle.Format = "R$ ###,###,##0.00"
        objBll = Nothing
    End Sub

    Private Sub bAjuda_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAjuda.Click
        Dim Ajuda As String
        Ajuda = Application.StartupPath
        Ajuda = Ajuda.Replace("\bin\Debug", "\Arquivo de Ajuda\ContasReceber.pdf")
        Dim Ie As Object
        Ie = CreateObject("InternetExplorer.Application")
        Ie.Visible = True
        Ie.Navigate(Ajuda)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Ajuda As String
        Ajuda = Application.StartupPath
        Ajuda = Ajuda.Replace("\bin\Debug", "\Arquivo de Ajuda\ContasReceber.pdf")
        Dim Ie As Object
        Ie = CreateObject("InternetExplorer.Application")
        Ie.Visible = True
        Ie.Navigate(Ajuda)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Ajuda As String
        Ajuda = Application.StartupPath
        Ajuda = Ajuda.Replace("\bin\Debug", "\Arquivo de Ajuda\ContasReceber.pdf")
        Dim Ie As Object
        Ie = CreateObject("InternetExplorer.Application")
        Ie.Visible = True
        Ie.Navigate(Ajuda)
    End Sub
End Class